﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Cwt;

namespace WebApp.Management._Article
{
    public partial class Create : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ICwtService service = Global.Get<ICwtService>();
                this._ctlListCategory.DataSource = service.FindCategories();
                this._ctlListCategory.DataBind();
            }
        }

        protected void _ctlBtnSave_Click(object sender, EventArgs e)
        {
            string id = Request.Form["id"];
            string title = Request.Form["title"];
            string postedDate = Request.Form["posteddate"];
            string author = Request.Form["author"];
            int toppoint = Convert.ToInt32(Request.Form["toppoint"]);
            string content = Request.Form["content"];
            string summary = Request.Form["summary"];
            string allowReplay = Request.Form["allowreplay"];
            string replayVisible = Request.Form["replayvisible"];
            string enabled = Request.Form["enabled"];
            Guid categoryId = new Guid(_ctlListCategory.SelectedValue);

            ICwtService service = Global.Get<ICwtService>();
            Article model = new Article();
            model.Id = Guid.NewGuid();
            model.Title = title;
            model.CategoryId = categoryId;
            model.PostedDate = Convert.ToDateTime(postedDate);
            model.AuthorId = Guid.Empty;
            model.Author = author;
            model.TopPoint = toppoint;
            model.Content = content;
            model.Summary = summary;
            model.IsAllowReplay = Convert.ToBoolean(allowReplay);
            model.IsReplayVisible = Convert.ToBoolean(replayVisible);
            model.IsEnabled = Convert.ToBoolean(enabled);

            if (service.SaveArticle(model))
            {
                Response.Redirect("./List.aspx");
            }
            else
            {
                Response.Redirect("../Message.aspx");
            }
        }
    }
}